package edu.wtbu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;

import edu.wtbu.dao.ScheduleDao;
import edu.wtbu.pojo.Page;
import edu.wtbu.pojo.Result;

public class ScheduleService {
	/*
	 * 获取航班状态
	 */
	public static Result getFlightStatus(String start, String end, int startPage, int pageSize) {
		List<HashMap<String, Object>> listSchedule = ScheduleDao.findScheduleByDate(start, end, startPage, pageSize);
		int total = ScheduleDao.scheduleByDateCount(start, end);
		Page page = new Page(total, startPage, pageSize);
		Result result = new Result("success", page, listSchedule);
		return result;
	}

	/*
	 * 获取航班计划（管理员）
	 */
	public static Result findScheduleByCityAndDate(String fromCity, String toCity, String startDate, String endDate) {
		List<HashMap<String, Object>> listSchedule = ScheduleDao.findScheduleByCityAndDate(fromCity, toCity, startDate,
				endDate);
		Result result = new Result("success", null, listSchedule);
		return result;
	}

	/*
	 * 航班计划状态修改
	 */
	public static Result updateSchedule(int scheduleId, String status) {
		Result result = new Result("fail", null, null);
		int updateResult = ScheduleDao.updateSchedule(scheduleId, status);
		if (updateResult > 0) {
			result.setFlag("success");
		}
		return result;
	}

	/*
	 * 获取机票售出详情
	 */
	public static Result findScheduleByScheduleId(int scheduleId) {
		Result result = new Result("fail", null, null);

		List<HashMap<String, Object>> listSchedule = ScheduleDao.findScheduleByScheduleId(scheduleId);
//		HashMap<String, Object> hashMap =listSchedule.get(0);
		if (listSchedule == null || listSchedule.size() == 0) {
			return result;
		}

		try {
			int aircraftId = Integer.parseInt(listSchedule.get(0).get("AircraftId").toString());
			List<HashMap<String, Object>> listTickets = ScheduleDao.findTicketsByScheduleId(scheduleId);
			List<HashMap<String, Object>> listSeat = ScheduleDao.findSeatByScheduleId(scheduleId);
			List<HashMap<String, Object>> listSeatLayout = ScheduleDao.findSeatByAircraftId(aircraftId);

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ListSchedule", listSchedule);
			map.put("ListTickets", listTickets);
			map.put("ListSeat", listSeat);
			map.put("ListSeatLayout", listSeatLayout);

			result.setFlag("success");
			result.setData(map);
			return result;
		} catch (Exception e) {
			return result;
		}
	}

	/*
	 * 获取航班计划（员工）
	 */
	public static Result getSearchFlight(String fromCity, String toCity, String startDepartureDate,
			String endDepartureDate, int cabinType, String flightType) {
		Result result = new Result();
		if (flightType.equals("All")) {
			List<HashMap<String, Object>> listNoStopSchedule = getNonStop(fromCity, toCity, startDepartureDate,
					endDepartureDate, cabinType);
			List<HashMap<String, Object>> listOneStopSchedule = getOneStop(fromCity, toCity, startDepartureDate,
					endDepartureDate, cabinType);
			listNoStopSchedule.addAll(listOneStopSchedule);

			result.setFlag("success");
			result.setData(listNoStopSchedule);
		}
		if(flightType.equals("Non-stop")) {
			List<HashMap<String,Object>> listNoStopSchedule=getNonStop(fromCity,toCity,startDepartureDate,endDepartureDate,cabinType);
			result.setFlag("success");
			result.setData(listNoStopSchedule);
		}
		if(flightType.equals("1-stop")) {
			List<HashMap<String,Object>> listOneStopSchedule=getOneStop(fromCity,toCity,startDepartureDate,endDepartureDate,cabinType);
			result.setFlag("success");
			result.setData(listOneStopSchedule);
		}
		return result;
	}

	private static List<HashMap<String, Object>> getOneStop(String fromCity, String toCity, String startDepartureDate,
			String endDepartureDate, int cabinType) {
		// TODO Auto-generated method stub
		List<HashMap<String, Object>> listSchedule=ScheduleDao.findScheduleOneStop(fromCity,toCity,startDepartureDate,endDepartureDate);
		for(int i=0;listSchedule!=null&&i<listSchedule.size();i++) {
			HashMap<String, Object> schedule=listSchedule.get(i);
			int scheduleId1=Integer.parseInt(listSchedule.get(i).get("S1ScheduleId").toString());
			int scheduleId2=Integer.parseInt(listSchedule.get(i).get("S2ScheduleId").toString());
			String flightNumber1=listSchedule.get(i).get("S1FlightNumber").toString();
			String flightNumber2=listSchedule.get(i).get("S2FlightNumber").toString();
			int AllCount1=ScheduleDao.findAllCount(flightNumber1,startDepartureDate);
			int AllCount2=ScheduleDao.findAllCount(flightNumber2,startDepartureDate);
			int Notdelay1=ScheduleDao.findNotdelay(flightNumber1,startDepartureDate);
			int Notdelay2=ScheduleDao.findNotdelay(flightNumber2,startDepartureDate);
			schedule.put("S1AllCount", AllCount1);
			schedule.put("S2AllCount", AllCount2);
			schedule.put("S1Notdelay", Notdelay1);
			schedule.put("S2Notdelay", Notdelay2);
			schedule.put("S1Delaycount", AllCount1-Notdelay1);
			schedule.put("S2Delaycount", AllCount2-Notdelay2);
			int ticketCounts1=ScheduleDao.findTicketCounts(scheduleId1,cabinType);
			int ticketCounts2=ScheduleDao.findTicketCounts(scheduleId2,cabinType);
			if(cabinType==1) {
				int allTickets1=Integer.parseInt(schedule.get("S1EconomySeatsAmount").toString());
				int allTickets2=Integer.parseInt(schedule.get("S1EconomySeatsAmount").toString());
				schedule.put("S1ResidueTickets", allTickets1-ticketCounts1);
				schedule.put("S2ResidueTickets", allTickets2-ticketCounts2);
			}
			if(cabinType==2) {
				int allTickets1=Integer.parseInt(schedule.get("S1BusinessSeatsAmount").toString());
				int allTickets2=Integer.parseInt(schedule.get("S2BusinessSeatsAmount").toString());
				schedule.put("S1ResidueTickets", allTickets1-ticketCounts1);
				schedule.put("S2ResidueTickets", allTickets2-ticketCounts2);
			}
			if(cabinType==3) {
				int allTickets1=Integer.parseInt(schedule.get("S1FirstSeatsAmount").toString());
				int allTickets2=Integer.parseInt(schedule.get("S2FirstSeatsAmount").toString());
				schedule.put("S1ResidueTickets", allTickets1-ticketCounts1);
				schedule.put("S2ResidueTickets", allTickets2-ticketCounts2);
			}
			schedule.put("FlightType","1-stop");
		}
		return listSchedule;
	}

	private static List<HashMap<String, Object>> getNonStop(String fromCity, String toCity,
			String startDepartureDate, String endDepartureDate, int cabinType) {
		// TODO Auto-generated method stub
		
		List<HashMap<String, Object>> listSchedule=ScheduleDao.findScheduleNoStop(fromCity,toCity,startDepartureDate,endDepartureDate);
		for(int i=0;listSchedule!=null&&i<listSchedule.size();i++) {
			HashMap<String, Object> schedule=listSchedule.get(i);
			int scheduleId=Integer.parseInt(listSchedule.get(i).get("scheduleId").toString());
			String flightNumber=listSchedule.get(i).get("FlightNumber").toString();
			//
			int AllCount=ScheduleDao.findAllCount(flightNumber,startDepartureDate);
			int Notdelay=ScheduleDao.findNotdelay(flightNumber,startDepartureDate);
			schedule.put("AllCount", AllCount);
			schedule.put("Notdelay", Notdelay);
			schedule.put("Delaycount", AllCount-Notdelay);
			// 
			int ticketCounts=ScheduleDao.findTicketCounts(scheduleId,cabinType);
			if(cabinType==1) {
				int allTickets=Integer.parseInt(schedule.get("EconomySeatsAmount").toString());
				schedule.put("ResidueTickets", allTickets-ticketCounts);
			}
		else  if(cabinType==2) {
				int allTickets=Integer.parseInt(schedule.get("BusinessSeatsAmount").toString());
				schedule.put("ResidueTickets", allTickets-ticketCounts);
			}
		else{
				int allTickets=Integer.parseInt(schedule.get("FirstSeatsAmount").toString());
				schedule.put("ResidueTickets", allTickets-ticketCounts);
			}
			schedule.put("FlightType","Non-stop");
		}
		return listSchedule;

	}

	
	
	
//	/*
//	 * 无中转
//	 */
//	protected static List<HashMap<String, Object>> getNonstop(String fromCity, String toCity, String startDepartureDate,
//			String endDepartureDate, int cabinType) {
//		List<HashMap<String, Object>> listSchedule = ScheduleDao.findSchedulesByCityAndDate(fromCity, toCity,
//				startDepartureDate, endDepartureDate);
//
//		for (int i = 0; listSchedule != null && i < listSchedule.size(); i++) {
//			HashMap<String, Object> map = listSchedule.get(i);
//			try {
//				int ScheduleId = Integer.parseInt(map.get("ScheduleId").toString());
//				List<HashMap<String, Object>> listResidueTickes = ScheduleDao
//						.findResidueTickesByScheduleIdAndCabinType(ScheduleId, cabinType);
//				int counts = Integer.parseInt(listResidueTickes.get(0).get("Counts").toString());
//				if (cabinType == 3) {
//					int allTickes = Integer.parseInt(map.get("FirstSeatsAmount").toString());
//					map.put("ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 2) {
//					int allTickes = Integer.parseInt(map.get("BusinessSeatsAmount").toString());
//					map.put("ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 1) {
//					int allTickes = Integer.parseInt(map.get("EconomySeatsAmount").toString());
//					map.put("ResidueTickes", allTickes - counts);
//				}
//				map.put("FlightType", "Non-stop");
//			} catch (Exception e) {
//				map.put("ResidueTickes", 0);
//			}
//		}
//		return listSchedule;
//	}
//
//	/*
//	 * 有中转
//	 */
//	protected static List<HashMap<String, Object>> getOnestop(String fromCity, String toCity, String startDepartureDate,
//			String endDepartureDate, int cabinType) {
//		List<HashMap<String, Object>> listSchedule = ScheduleDao.findOnestopSchedulesByCityAndDate(fromCity, toCity,
//				startDepartureDate, endDepartureDate);
//		List<HashMap<String, Object>> listFlightNumber = ScheduleDao.findFlightNumber(startDepartureDate);
//		for (int i = 0; listSchedule != null && i < listSchedule.size(); i++) {
//			HashMap<String, Object> map = listSchedule.get(i);
//			try {
//				int s1scheduleId = Integer.parseInt(map.get("S1ScheduleId").toString());
//				int s2scheduleId = Integer.parseInt(map.get("S2ScheduleId").toString());
//				String s1flightnumber = map.get("S1FlightNumber").toString();
//				String s2flightnumber = map.get("S2FlightNumber").toString();
//				for (int j = 0; listFlightNumber != null && j < listFlightNumber.size(); j++) {
//					String flightnumber = listFlightNumber.get(j).get("FlightNumber").toString();
//					if (flightnumber.equals(s1flightnumber)) {
//						map.put("S1AllCount", listFlightNumber.get(j).get("FlightNumber").toString());
//						map.put("S1DelayCount", listFlightNumber.get(j).get("DelayCount").toString());
//						map.put("S1NotDelay", listFlightNumber.get(j).get("NotDelay").toString());
//					}
//					if (flightnumber.equals(s2flightnumber)) {
//						map.put("S2AllCount", listFlightNumber.get(j).get("FlightNumber").toString());
//						map.put("S2DelayCount", listFlightNumber.get(j).get("DelayCount").toString());
//						map.put("S2NotDelay", listFlightNumber.get(j).get("NotDelay").toString());
//					}
//				}
//				List<HashMap<String, Object>> listResidueTickes = ScheduleDao
//						.findResidueTickesByScheduleIdAndCabinType(s1scheduleId, cabinType);
//				int counts = Integer.parseInt(listResidueTickes.get(0).get("Counts").toString());
//				if (cabinType == 3) {
//					int allTickes = Integer.parseInt(map.get("S1FirstSeatsAmount").toString());
//					map.put("S1ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 2) {
//					int allTickes = Integer.parseInt(map.get("S1BusinessSeatsAmount").toString());
//					map.put("S1ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 1) {
//					int allTickes = Integer.parseInt(map.get("S1EconomySeatsAmount").toString());
//					map.put("S1ResidueTickes", allTickes - counts);
//				}
//
//				listResidueTickes = ScheduleDao.findResidueTickesByScheduleIdAndCabinType(s2scheduleId, cabinType);
//				counts = Integer.parseInt(listResidueTickes.get(0).get("Counts").toString());
//				if (cabinType == 3) {
//					int allTickes = Integer.parseInt(map.get("S2FirstSeatsAmount").toString());
//					map.put("S2ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 2) {
//					int allTickes = Integer.parseInt(map.get("S2BusinessSeatsAmount").toString());
//					map.put("S2ResidueTickes", allTickes - counts);
//				}
//				if (cabinType == 1) {
//					int allTickes = Integer.parseInt(map.get("S2EconomySeatsAmount").toString());
//					map.put("S2ResidueTickes", allTickes - counts);
//				}
//				map.put("FlightType", "1-stop");
//			} catch (Exception e) {
//
//			}
//		}
//		return listSchedule;
//	}
}
